package com.tingyukeji.fastpay.sport.view;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.tingyukeji.fastpay.R;
import com.tingyukeji.fastpay.databinding.CountDownLayoutBinding;
import com.tingyukeji.fastpay.sport.utils.MyCountTimer;
import com.tingyukeji.fastpay.utils.AnimationUtils;

/**
 * Created by caik on 2017/3/20.
 */

public class CountdownDialog extends DialogFragment {
    private CountDownLayoutBinding binding;
//    private ImmersionBar mImmersionBar;

    public static CountdownDialog newInstance() {
        CountdownDialog fragment = new CountdownDialog();
        return fragment;
    }

    private View mRootView;
    private OnClickListener mListener;

    public void setListener(OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.dimAmount =0.2f;
        params.gravity = Gravity.BOTTOM;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.getDecorView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dismiss();
                return false;
            }
        });

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DataBindingUtil.inflate(inflater, R.layout.count_down_layout, container, false);
        mRootView = binding.getRoot();
        initData();
        AnimationUtils.slideToUp(mRootView);
//        mImmersionBar = ImmersionBar.with(this, getDialog())
//                .statusBarDarkFont(true,0.2f)   //状态栏字体是深色，不写默认为亮色
//                .transparentStatusBar();
//        mImmersionBar.init();
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initData() {
        MyCountTimer myCountTimer = new MyCountTimer(4000, 1000, binding.btnCountTimer, "");
        myCountTimer.start();
        myCountTimer.setListener(new MyCountTimer.OnFinishListener() {
            @Override
            public void onCountDown() {
                mListener.finish();
                dismiss();
            }
        });
    }

    @Override
    public void dismiss() {
        AnimationUtils.slideToDown(mRootView, new AnimationUtils.AnimationListener() {
            @Override
            public void onFinish() {
                CountdownDialog.super.dismiss();
            }
        });
    }

    public interface OnClickListener {
        void click(String message);
        void finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        if (mImmersionBar != null)
//            mImmersionBar.destroy();
    }
}
